package com.myself.core.service

import com.myself.common.repository.BaseRepository
import com.myself.common.service.BaseService
import com.myself.core.entity.Record
import com.myself.core.repository.RecordRepository
import com.myself.core.table.Records
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import kotlin.random.Random


@Service
open class RecordService(
    private val repository: RecordRepository
) : BaseService<Record, Records>(repository) {

    @Scheduled(cron = "0 0/10 * * * ?")
    open fun generateWinningNumbers() {
        // 使用 Set 来确保唯一性
        val winningNumbers = mutableSetOf<Int>()

        // 生成不重复的 3 个中奖号码
        while (winningNumbers.size < 3) {
            winningNumbers.add((1..11).random())
        }

        // 将生成的三个不重复的号码存入记录
        repository.addOne(Record {
            // 使用 iterator 获取生成的号码
            val iterator = winningNumbers.iterator()
            this.first = iterator.next()
            this.second = iterator.next()
            this.third = iterator.next()
        })
    }

}